<template>
  <div class="panel">
    <el-dialog
      title="新增员工"
      :visible="showDialog"
      @close="cancel"
      :close-on-click-modal="false"
    >
      <el-form :model="form" :rules="rules" ref="myform" label-width="100px">
        <el-form-item label="姓名" prop="username">
          <el-input
            v-model="form.username"
            autocomplete="off"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input
            v-model="form.mobile"
            autocomplete="off"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select
            v-model="form.formOfEmployment"
            placeholder="请选择聘用形式"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input
            v-model="form.workNumber"
            autocomplete="off"
            placeholder="请输入工号"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div class="box">
            <el-input
              v-model="form.departmentName"
              @focus="myfocus"
              autocomplete="off"
              placeholder="请选择部门"
            ></el-input>
            <!-- 部门选择 -->
            <el-tree
              class="treeBox"
              v-if="showTree"
              :data="deptsList"
              :props="{ label: 'name' }"
              default-expand-all
              :expand-on-click-node="false"
              @node-click="nodeClick"
            ></el-tree>
          </div>
        </el-form-item>
        <el-form-item label="转正日期" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitData">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入聘用形式的枚举
import myemployees from '@/api/constant/employees'
// 导入获取组织架构的接口
import { getDepartment } from '@/api/departments'
// 导入新增员工接口
import { addEmployees } from '@/api/employees'
export default {
  props: ['showDialog'],
  data () {
    return {
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' //转正时间
      },
      // 下拉框中的数据源
      typeList: myemployees.hireType,
      deptsList: [{ name: '深圳', children: [{ name: '南山' }] }],
      showTree: false, // 控制部门下拉框显示与隐藏
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              //   手机号码格式校验
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'blur'
          }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //   关闭面板
    cancel () {
      this.$emit('update:showDialog', false)
      this.$refs.myform.resetFields()
    },
    // 部门选择获取焦点触发
    myfocus () {
      this.showTree = true
    },
    // 获取组织架构接口方法
    async getDepartments () {
      let res = await getDepartment()
      // console.log(res);
      this.deptsList = this.transformData(res.data.data.depts, '')
    },
    // 转换数据的方法(转换为树状结构)
    transformData (arr, pid) {
      let newArr = []
      arr.forEach(item => {
        if (item.pid == pid) {
          let children = this.transformData(arr, item.id)
          if (children.length >= 0) {
            item.children = children
          }
          newArr.push(item)
        }
      })
      return newArr
    },
    // 部门树状节点被点击时触发的方法
    nodeClick (data) {
      this.form.departmentName = data.name
      this.showTree = false
    },
    // 新增员工方法
    submitData () {
      // 全局校验
      this.$refs.myform.validate(async val => {
        if (val) {
          // 校验成功, 发请求
          await addEmployees(this.form)
          //  关闭面板
          this.$emit('update:showDialog', false)
          // 清空数据
          this.form = {
            username: '',
            mobile: '',
            timeOfEntry: '',
            formOfEmployment: '',
            workNumber: '',
            departmentName: '',
            correctionTime: ''
          }
          //   提示
          this.$message.success('新增员工成功')
          // 重置校验
          this.$refs.myform.resetFields()
          // 更新数据
          this.$emit('updateDate')
        }
      })
    }
  },
  created () {
    this.getDepartments()
  }
}
</script>

<style lang="scss">
.panel {
  .el-dialog__body {
    // text-align: center;
    .el-form {
      width: 600px;
      margin: 0 auto;
      .el-form-item__label {
        text-align: left;
      }
    }
  }
  .box {
    position: relative;
    width: 300px;
    .treeBox {
      position: absolute;
      top: 46px;
      left: 0;
      z-index: 99;
      width: 100%;
      background-color: #ffffff;
      border: 1px solid #ddd;
      border-radius: 3px;
      .el-tree-node__content {
        height: 40px;
        .el-tree-node__expand-icon {
          display: none;
        }
        .el-tree-node__label {
          margin-left: 16px;
        }
      }
    }
  }
}
</style>
